package test.freshui.ffmpeg;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PlayerFileView extends ListActivity {
	static private final String TAG = "playerFileView" ;
	public static final String FILE_PATH	= "file_path";
	public static final String FILE_INDEX	= "file_index";
	public static final String FILE_PATHS	= "file_paths";//all file in current directory
	
    /** Called when the activity is first created. */
    private ArrayList<String> paths = null;//   
    private String rootPath = "/sdcard"; 
    private String curPath = "/sdcard"; 

 
    @Override  
    public void onCreate(Bundle savedInstanceState)  {   
        super.onCreate(savedInstanceState);   
        getFileDir(rootPath);//rootPath.   
    }   
    
    public void getFileDir(String filePath) {   
        try
        {   
            paths = new ArrayList<String>();   
            File f = new File(filePath);   
            File[] files = f.listFiles();//    

            // list   
            if(files != null) {   
            	ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); 
            		
            	//cureent is root path
                if(!filePath.equals(rootPath)) {
                	HashMap<String, Object> map = new HashMap<String, Object>();  
                    
                	map.put("image", R.drawable.dir);
                    map.put("text", "../");     
		            listItem.add(map);  
		            paths.add("../"); 
                }
                
                for (int i = 0; i < files.length; i++) {   
                    File file = files[i];   
                    HashMap<String, Object> map = new HashMap<String, Object>();  
                    
                    if(file.isDirectory()) {
                    	map.put("image", R.drawable.dir);
                    }else {
                    	map.put("image", R.drawable.file);
                    }
                    
                    map.put("text", file.getName());     
		            listItem.add(map);  
		                
		            paths.add(file.getPath());   
                }   
                
                SimpleAdapter listItemAdapter = new SimpleAdapter(this,
								                	listItem,  
									                R.layout.sd_list_item,
									                new String[] {"image","text"},    
									                new int[] {R.id.sd_item_image,R.id.sd_item_text}); 
                this.setListAdapter(listItemAdapter);  
            }  
            
            curPath = filePath;
            setTitle(filePath);
        }
        catch(Exception ex){   
            ex.printStackTrace();   
        }   
    }   
  
    @Override  
    protected void onListItemClick(ListView l, View v, int position, long id) {   
        if(( 0 == position ) && (!curPath.equals(rootPath)))
        {
        	backToParentDir();
        }
        else
        {
        	 String path = paths.get(position);   
             File file = new File(path);   
      
             if(file.isDirectory())
             {   
            	 getFileDir(path); 
             }
             else//is a file
             {   
            	 /* for normal activity */
            	 Intent intent = new Intent(PlayerFileView.this, PlayerUI.class);
            	 Bundle bundle  = new Bundle();
            	 bundle.putString(FILE_PATH, path);
            	 bundle.putInt(FILE_INDEX, position);
            	 bundle.putStringArrayList(FILE_PATHS, paths);
            	 intent.putExtras(bundle);
            	 startActivity(intent);
            	 
            	 /* for onResultActivity */
            	 //setResult(RESULT_OK, (new Intent()).setAction(path));
            	 //finish();
             } 
        }
        super.onListItemClick(l, v, position, id);  
    }  

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {
		if (keyCode == KeyEvent.KEYCODE_BACK){
			return true;
		} 

		return super.onKeyDown(keyCode, msg);
	}
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent msg){
		if (keyCode == KeyEvent.KEYCODE_BACK){
			if(curPath.equals(rootPath)){
				this.finish();
			}else{
				backToParentDir();
			}
			return true;
		} 

		return super.onKeyUp(keyCode, msg);
	}
    
    private void backToParentDir()    {
    	String pre_path;
    	
    	if(!curPath.equals(rootPath))
		{
			if( 0 == curPath.lastIndexOf('/') ){
				pre_path = rootPath;
			}
			else
			{
				pre_path = curPath.substring(0, curPath.lastIndexOf('/'));
			}
			getFileDir(pre_path);
		}
    }
}

